#[[
 * Copyright (c) 2024 iSOFT INFRASTRUCTURE SOFTWARE CO., LTD.
 * easyAda is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
]]

cmake_minimum_required (VERSION 3.5)
set (easyAda_VERSION_MAJOR 1)
set (easyAda_VERSION_MINOR 0)

include(cmake/config.cmake)
generate_toolchainfile()
project (easyAda)

include(kernel/arch/${KernelArch}/config.cmake)
include(kernel/config.cmake)
include(kernel/drivers/config.cmake)

include_directories(
    "include"
    "include/plat/${KernelPlatform}/"
    "include/arch/${KernelArch}"
)


if(ArmCortexA53)
    message("Arm CortexA53")
    add_compile_options(-mcpu=cortex-a53)
elseif(ArmCortexA55)
    message("Arm CortexA55")
    add_compile_options(-mcpu=cortex-a55)
elseif(ArmCortexA57)
    message("Arm CortexA57")
    add_compile_options(-mcpu=cortex-a57)
elseif(ArmCortexA72)
    message("Arm CortexA72")
    add_compile_options(-mcpu=cortex-a72)
else()
    message(FATAL_ERROR "Unsupported ARM CPU")
endif()


add_custom_command(
    OUTPUT linker
    COMMAND ${CMAKE_C_COMPILER}  -E -x c -P  -I "${CMAKE_CURRENT_SOURCE_DIR}/include/plat/${KernelPlatform}/" ${CMAKE_CURRENT_SOURCE_DIR}/kernel_linker.lds -o linker.lds
    DEPENDS kernel_linker.lds 
)
add_custom_target(linker_script ALL DEPENDS linker)


set(target "kernel")
set_property(SOURCE ${c_sources} ${asm_sources} PROPERTY LANGUAGE C)
add_executable(${target}.elf ${c_sources} ${asm_sources})
target_link_options(${target}.elf PRIVATE -Wl,-T linker.lds -Wl,-Map ${target}.elf.map)
add_dependencies(${target}.elf linker_script)


add_custom_command(
    OUTPUT ${target}.bin
    COMMAND ${CMAKE_OBJCOPY}  -O binary ${target}.elf ${target}.bin
    DEPENDS ${target}.elf 
)
add_custom_target(${target} ALL DEPENDS ${target}.bin)
add_dependencies(${target} ${target}.elf)
